<?php

namespace plugin\ycMassage\app\admin\controller;

use app\common\builder\ListBuilder;
use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageUserWechat;
use support\Request;

class UserWechatController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addScreen('uid', 'input', 'UID');
        $builder->addScreen('mobile', 'input', '手机号');
        $builder->addScreen('openid', 'input', '公众号OpenID');
        $builder->addScreen('mp_openid', 'input', '小程序OpenID');
        $builder->addScreen('unionid', 'input', 'UnionID');
        $builder->addColumn('id', '序号', [
            'width' => 90
        ])
            ->addColumn('uid', 'UID', [
                'width' => 90
            ])
            ->addColumn('mobile', '手机号', [
                'width' => 120
            ])
            ->addColumnEle('headimg', '头像', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('nickname', '昵称', [
                'width' => 120
            ])
            ->addColumn('openid', '公众号OpenID')
            ->addColumn('mp_openid', '小程序OpenID')
            ->addColumn('unionid', 'UnionID')
            ->addColumn('create_time', '授权时间');
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['u.saas_appid', '=', $request->saas_appid]
        ];
        $uid = $request->get('uid');
        if ($uid) {
            $where[] = ['a.uid', '=', $uid];
        }
        $mobile = $request->get('mobile');
        if ($mobile) {
            $where[] = ['u.mobile', 'like', "%$mobile%"];
        }
        $openid = $request->get('openid');
        if ($openid) {
            $where[] = ['a.openid', 'like', "%$openid%"];
        }
        $mp_openid = $request->get('mp_openid');
        if ($mp_openid) {
            $where[] = ['a.mp_openid', 'like', "%$mp_openid%"];
        }
        $unionid = $request->get('unionid');
        if ($unionid) {
            $where[] = ['a.unionid', 'like', "%$unionid%"];
        }
        $data = MassageUserWechat::where($where)
            ->alias('a')
            ->join('massage_user u', 'a.uid=u.id', 'LEFT')
            ->order('id desc')
            ->field('a.*,u.mobile')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        return $this->successRes($data);
    }
}
